package com.ccx.demo.business.code.controller;

import com.ccx.demo.business.code.entity.TabCodeSearchId;
import com.ccx.demo.business.code.service.CodeSearchIdService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.support.mvc.encrypt.AesApiId;
import com.support.mvc.entity.IUser;
import com.support.mvc.entity.base.Page;
import com.support.mvc.entity.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 对外接口：测试 SearchId 模板表
 *
 * @author 谢长春 on 2022-02-17 V20220301
 */
@Api(tags = "测试 SearchId 模板表", hidden = true)
//@ApiSort() // 控制接口排序
@RequestMapping("/open/code-search-id")
@Controller
@Slf4j
@RequiredArgsConstructor
public class OpenCodeSearchIdController {

    private final CodeSearchIdService service;

    @GetMapping("/page/{number}/{limit}")
    //@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_ADMIN_TEST', '{}_load')")
    @ApiOperation(value = "1.分页查询测试 SearchId 模板表", tags = {""})
    @ApiOperationSupport(
            order = 1,
            author = "谢长春",
            includeParameters = {"number", "limit", "orderBy", "fromId", "toId"}
    )
    @ResponseBody
    public Result<TabCodeSearchId> page(
            @ApiIgnore @AuthenticationPrincipal final IUser user,
            @ApiParam(required = true, value = "页码", example = "1") @PathVariable final int number,
            @ApiParam(required = true, value = "每页条数，最大值1000", example = "10") @PathVariable final int limit,
            final TabCodeSearchId condition) {
        return new Result<TabCodeSearchId>().execute(result -> { // Controller 方法逻辑写在这里
            result.setSuccess(service.page(condition, Page.of(number, limit)));
        });
    }

//// 非必要情况下不要开放列表查询方法，因为没有分页控制，容易内存溢出。大批量查询数据应该使用分页查询
//    @GetMapping
//    // @PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_ADMIN_TEST', '{}_load')")
//    @ApiOperation(value = "2.列表查询测试 SearchId 模板表", tags = {""})
//    @ApiOperationSupport(
//            order = 2,
//            author = "谢长春",
//            includeParameters = {"orderBy", "fromId", "toId"}
//    )
//    @ResponseBody
//    public Result<TabCodeSearchId> list(@ApiIgnore @AuthenticationPrincipal final IUser user, final TabCodeSearchId condition) {
//        return new Result<TabCodeSearchId>().execute(result -> { // Controller 方法逻辑写在这里
//            result.setSuccess(service.list( condition ));
//        });
//    }
//

    @GetMapping("/{encryptId}")
    //@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_ADMIN_TEST', '{}_load')")
    @ApiOperation(value = "3.按 id 查询测试 SearchId 模板表", tags = {""})
    @ApiOperationSupport(order = 3, author = "谢长春")
    @ResponseBody
    public Result<TabCodeSearchId> findById(
            @ApiIgnore @AuthenticationPrincipal final IUser user,
            @ApiParam(required = true, value = "加密id", example = "encryptId") @PathVariable final String encryptId
    ) {
        return new Result<TabCodeSearchId>().execute(result -> { // Controller 方法逻辑写在这里
            final AesApiId.AesId decrypt = AesApiId.decrypt(encryptId);
            result.setSuccess(service.findById(decrypt.getStringId()).orElse(null));
        });
    }

}
